<template>
  <div class="home">
    <template v-for="item in list">
      <van-card :key="item.id" :price="item.price" :desc="item.desc" :title="item.title" :thumb="item.img">
        <template #footer>
          <van-button size="mini" @click="handleToCart(item)">+</van-button>
        </template>
      </van-card>
    </template>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'HomeView',
  data() {
    return {
      list: [],
      cartList: [],
    }
  },
  components: {},
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await axios.get('/api/cartList', {
        params: {
          _page: 1,
          _limit: 5,
        },
      })
      this.list = res.data
      console.log('res:', res.data)
    },
    // 加入购物车
    handleToCart(item) {
      this.$store.commit('TO_CART', item)
      // this.$toast({
      //   type: 'success',
      //   message: '添加成功',
      //   icon: 'success'
      // });
      // localStorage.setItem('toCart', JSON.stringify(this.cartList))
      // EventBus.$emit('toCart', this.cartList)
      //  console.log(this.cartList, '购物车数据')
    },
  },
}
</script>
